package com.horse.three60;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.taptwo.android.widget.TitleProvider;
import org.taptwo.android.widget.ViewFlow;

import com.horse.utils.Constants;
import com.horse.utils.Downloader;
import com.horse.utils.Utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Animation.AnimationListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewAnimator;

public class VettalkActivity extends Activity implements OnClickListener {

	private ImageView viewButton;

	private TextView backButton;

	private ImageView imageView;

	private TextView nameView;

	private TextView tapView;

	private boolean isBack;

	private int posX;
	private int posY;

	private boolean isZoomed;

	private RelativeLayout mainLayout;
	private int requestedLayer = 0;

	private String DIRECTION_RIGHT = "right";
	private String DIRECTION_LEFT = "left";
	private String DIRECTION_NO = "no";

	private String SKELETON_SIDE = "Skeleton Side";

	private String SKELETON_FRONT = "Skeleton Front";

	private String SKELETON_BACK = "Skeleton Back";

	private String MUSCLE_LAYER = "Muscle";

	private String MUSCLE_FRONT = "Muscle front";

	private String MUSCLE_BACK = "Muscle Back";

	private String SKIN_LAYER = "Skin";

	private String FORLIB_LAYER = "Forlimb";

	private String HOOF_UNDER_LAYER = "Hoof Under";

	private String HOOF_SIDE_LAYER = "Hoof Side";

	private String TENDON_LAYER = "Tendon";

	private String JOINTS_LAYER = "Joints";

	private String LIGAMENTS_LAYER = "Ligaments";

	private String HEAD_MUSCLE_LAYER = "Head Muscle";

	private String HEAD_BONE_LAYER = "Head Bone";

	private String TEETH_LAYER = "Teeth";

	private String INTESTINE_SIDE = "Intestine Side";

	private String INTESTINE_FRONT_LAYER = "Intestine Front";

	private String INTESTINE_BACK_LAYER = "Intestine Back";

	private String NECK_LAYER = "neck";

	private String HEART_LAYER = "Heart";

	private String LARGER_ORGAN = "Larger organs";

	private String HEART_ARTERIES_LAYER = "Heart arteries";

	private String LARGER_ORGANS_LAYER = "Larger Organs";

	private String CONFORMATION_LAYER = "Conformation";

	private String CONFORMATION_FRONT = "Conformation front";

	private String CONFORMATION_SIDE_FRONT = "Conformation Side Front";

	private String CONFORMATION_HIND = "Conformation Hind";

	private String CONFORMATION_SIDE_HIND = "Conformation Side Hind";

	private String MUSCLE_SIDE = "Muscle Side";

	private String SKIN_FRONT = "Skin Front";

	private String SKIN_SIDE_LAYER = "Skin side";

	private String SKIN_BACK = "Skin Back";

	private String lastLayer = "";

	private int requestedSection = 0;

	private List<JSONArray> items;
	private JSONArray ja;

	private ViewFlow viewFlow;
	private EfficientAdapter adapter;
	private ViewAnimator viewAnimator;
	private int imageViewWidth;
	private int imageViewHeight;
	private float factor;

	private List<JSONArray> pointList;

	private boolean isNormalState;

	private String tapText = "Tap on a highlighted part for its name";

	private String state = "tap";

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.layout_vettalk);
			viewAnimator = (ViewAnimator) this.findViewById(R.id.view_flipper);
			viewFlow = (ViewFlow) findViewById(R.id.viewflow);
			mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);

			viewButton = (ImageView) findViewById(R.id.viewButton);
			viewButton.setOnClickListener(this);
			// viewButton.setOnTouchListener(this);
			tapView = (TextView) findViewById(R.id.tapView);
			tapView.setVisibility(View.INVISIBLE);
			backButton = (TextView) findViewById(R.id.backButton);
			backButton.setOnClickListener(this);
			nameView = (TextView) findViewById(R.id.nameView);
			imageView = (ImageView) findViewById(R.id.imageView);
			imageView.setOnClickListener(this);

			new AsyncTask<String, String, String>() {

				ProgressDialog progressDialog;

				@Override
				protected void onPreExecute() {
					try {

						progressDialog = Utils.getDialog(VettalkActivity.this,
								"Please wait ...");
						progressDialog.show();

					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				protected String doInBackground(String... arg0) {
					try {
						items = getJsonArray();
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {

						viewAnimator.post(new Runnable() {

							@Override
							public void run() {
								factor = Math.min(
										viewAnimator.getWidth() / 480.0f,
										viewAnimator.getHeight() / 320.0f);
								imageView.getLayoutParams().width = imageViewWidth = (int) (480 * factor);
								imageView.getLayoutParams().height = imageViewHeight = (int) (320 * factor);
								adapter = new EfficientAdapter(
										VettalkActivity.this);
								viewFlow.setAdapter(adapter);
								viewFlow.setOnViewSwitchListener(new ViewFlow.ViewSwitchListener() {
									@Override
									public void onSwitched(View view,
											int position) {
										// TODO Auto-generated method stub
										nameView.setText(adapter
												.getTitle(position));
									}
								});
								viewFlow.setSelection(0);

							}

						});
						progressDialog.dismiss();
						tapView.setVisibility(View.VISIBLE);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();

			imageView.setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(View arg0, MotionEvent event) {
					switch (event.getAction()) {
					case MotionEvent.ACTION_DOWN:
						x1 = event.getX();
						y1 = event.getY();
						break;
					case MotionEvent.ACTION_UP:
						x2 = event.getX();
						y2 = event.getY();
						float dx = x2 - x1;
						if ((dx > 20) || (dx < -20)) {

							tapView.setVisibility(View.VISIBLE);
							if (state.equalsIgnoreCase("normal")) {
								viewAnimator.showNext();
							}
							// adapter.switchLayer();
							int selectionIndex = viewFlow
									.getSelectedItemPosition();
							if (dx < -20) {
								++selectionIndex;
							} else {
								--selectionIndex;
							}
							if ((selectionIndex >= adapter.getCount())
									|| (selectionIndex < 0)) {
								selectionIndex = 0;
							}
							viewFlow.setSelection(selectionIndex);
							adapter.notifyDataSetChanged();
							nameView.setText(adapter.getTitle(selectionIndex));
							tapView.setText("Tap on area to zoom in or Swipe for a different view");
							state = "tap";

						} else {
							posX = (int) (event.getX() / factor);
							posY = (int) (event.getY() / factor);
							parseJsonAndShow(posX, posY);
						}
					}
					return false;
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onClick(View v) {
		try {
			switch (v.getId()) {
			case R.id.viewButton:
				if (state.equalsIgnoreCase("normal")) {
					viewAnimator.showNext();
				}
				adapter.switchLayer();
				int selectionIndex = viewFlow.getSelectedItemPosition();
				if (selectionIndex >= adapter.getCount()) {
					selectionIndex = 0;
					viewFlow.setSelection(selectionIndex);
				}
				nameView.setText(adapter.getTitle(selectionIndex));
				state = "tap";
				tapView.setVisibility(View.VISIBLE);
				tapView.setText("Tap on an area to zoom or sweep for a different view");
				break;
			case R.id.backButton:
				if (state.equalsIgnoreCase("normal")) {
					viewAnimator.showNext();

					int index = viewFlow.getSelectedItemPosition();
					viewFlow.setSelection(index);
					nameView.setText(adapter.getTitle(index));
					
					tapView.setVisibility(View.VISIBLE);
					tapView.setText("Tap on an area to zoom or sweep for a different view");
					state = "tap";
				} else {
					goToHomeScreen();
				}
				break;

			default:
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private boolean isMusclesDownloaded;
	private boolean isHoovesDownloaded;
	private boolean isTendonsDownloaded;
	private boolean isHeadDownloaded;
	private boolean isOrgansDownloaded;

	private long transitionDuration = 500;

	private String DIRECTORY = "";

	private void tappedAnimationForLayer(String fileName) {
		try {
			Bitmap bm = null;
			boolean isInRaw = isInRaw(fileName);
			if (isInRaw) {
				int imageId = getResources().getIdentifier(
						Utils.getFileName(fileName), "raw",
						getClass().getPackage().getName());
				bm = BitmapFactory.decodeResource(getResources(), imageId);
			} else {
				bm = BitmapFactory.decodeFile(getFilePath(fileName));
			}
			Bitmap ret = Bitmap.createBitmap(imageViewWidth, imageViewHeight,
					Bitmap.Config.ARGB_8888);
			Canvas comboImage = new Canvas(ret);
			Rect r1 = new Rect(0, 0, bm.getWidth(), bm.getHeight());
			Rect r2 = new Rect(0, 0, imageViewWidth, imageViewHeight);
			comboImage.drawBitmap(bm, r1, r2, null);
			comboImage.save();
			imageView.setImageBitmap(ret);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void showAnimation() {
		tapView.setVisibility(View.VISIBLE);
		tapView.setText(tapText);
		Animation fadeIn = new AlphaAnimation(0, 1);
		fadeIn.setInterpolator(new DecelerateInterpolator());
		fadeIn.setDuration(200);

		Animation fadeOut = new AlphaAnimation(1, 0);
		fadeOut.setInterpolator(new AccelerateInterpolator());
		fadeOut.setStartOffset(200 + 1000);
		fadeOut.setDuration(200);

		AnimationSet animation = new AnimationSet(false);
		animation.addAnimation(fadeIn);
		animation.addAnimation(fadeOut);
		animation.setRepeatCount(0);
		animation.setFillAfter(false);
		tapView.setAnimation(animation);
		tapView.setVisibility(View.INVISIBLE);

	}

	private void parseJsonAndShow(int posX, int posY) {
		try {

			Log.i("DREG", "File Name=" + jsonFile);
			pointList = new ArrayList<JSONArray>();

			JSONObject jo = new JSONObject(new String(Utils.getFileData(this,
					jsonFile)));
			JSONArray ja = jo.getJSONObject("root").getJSONArray("horse");
			for (int i = 0; i < ja.length(); i++) {
				JSONArray points = ja.getJSONObject(i).getJSONArray("points");
				pointList.add(points);
			}

			int targetPosition = -1;
			for (int j = 0; j < ja.length(); j++) {
				JSONArray points = pointList.get(j);
				for (int k = 0; k < points.length(); k++) {
					int x1 = (int) (points.getJSONObject(k).getInt("-x1"));
					int x2 = (int) (points.getJSONObject(k).getInt("-x2"));
					int y1 = (int) (points.getJSONObject(k).getInt("-y1"));
					int y2 = (int) (points.getJSONObject(k).getInt("-y2"));
					if ((posX >= x1 && posX <= x2)
							&& (posY >= y1 && posY <= y2)) {
						targetPosition = j;
						break;
					}
				}
			}

			if (targetPosition != -1) {
				String part = ja.getJSONObject(targetPosition).getString(
						"-part");
				if((!part.endsWith("2")||(!part.endsWith("3"))))
				{
					nameView.setText(part);
				}
				else{
					nameView.setText(part.substring(0, part.length()-1));
				}
				String fileName = "VT_" + part;
				tappedAnimationForLayer(fileName);
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private String jsonFile = "vetTalk_skin_body.json";

	private void beginParsingForSection(int requestedSection,
			String requestedLayer) {

		if (requestedLayer.equalsIgnoreCase("Skeleton")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_skeletal_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_skeletal_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_skeletal_body2.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_skeletal_front.json";
			} else if (requestedSection == 5) {
				jsonFile = "vetTalk_skeletal_front2.json";
			} else if (requestedSection == 6) {
				jsonFile = "vetTalk_skeletal_back.json";
			} else if (requestedSection == 7) {
				jsonFile = "vetTalk_skeletal_back2.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Muscle")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_muscle_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_muscle_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_muscle_body2.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_muscle_front.json";
			} else if (requestedSection == 5) {
				jsonFile = "vetTalk_muscle_front2.json";
			} else if (requestedSection == 6) {
				jsonFile = "vetTalk_muscle_back.json";
			} else if (requestedSection == 7) {
				jsonFile = "vetTalk_muscle_back2.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Skin")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_skin_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_skin_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_skin_body2.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_skin_front.json";
			} else if (requestedSection == 5) {
				jsonFile = "vetTalk_skin_back.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Forelimb")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_forelimb_top.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_forelimb_bottom.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Hoof Under")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_hoofUnder.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Hoof Side")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_hoofSide_top.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_hoofSide_bottom.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Tendon")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_tendon_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_tendon_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_tendon_legs.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Joints")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_joints_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_joints_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_joints_front_legs.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_joints_back_legs.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Ligaments")) {

			if (requestedSection == 1) {
				jsonFile = "vetTalk_ligaments_head.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_ligaments_body.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_ligaments_front_legs.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_ligaments_back_legs.json";
			}

		} else if (requestedLayer.equalsIgnoreCase("Head Muscle")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_head_muscle.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Head Bone")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_head_bone.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Teeth")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_teeth.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Intestine Side")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_intestine_side.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Intestine Front")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_intestine_front.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Intestine Back")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_intestine_back.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Neck")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_neck.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Heart")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_heart_blue.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_heart_red.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Larger Organs")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_trachea.json";
			}
		} else if (requestedLayer.equalsIgnoreCase("Conformation")) {
			if (requestedSection == 1) {
				jsonFile = "vetTalk_conform_front.json";
			} else if (requestedSection == 2) {
				jsonFile = "vetTalk_conform_front_second.json";
			} else if (requestedSection == 3) {
				jsonFile = "vetTalk_conform_side_front.json";
			} else if (requestedSection == 4) {
				jsonFile = "vetTalk_conform_side_front_second.json";
			} else if (requestedSection == 7) {
				jsonFile = "vetTalk_conform_side_hind.json";
			} else if (requestedSection == 8) {
				jsonFile = "vetTalk_conform_side_hind_second.json";
			} else if (requestedSection == 5) {
				jsonFile = "vetTalk_conform_hind.json";
			} else if (requestedSection == 6) {
				jsonFile = "vetTalk_conform_hind_second.json";
			}
		}

	}

	private String getJString() {
		String jsonFileName;
		jsonFileName = "Vettalk Layer.json";
		try {
			JSONArray ja = new JSONArray(new String(Utils.getFileData(this,
					jsonFileName)));
			Log.i("DREG", "jsonString=" + ja.toString());
			return ja.toString();
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
	}

	private List<JSONArray> getJsonArray() {
		List<JSONArray> items = new ArrayList<JSONArray>();
		try {
			ja = new JSONArray(new String(Utils.getFileData(this,
					"Vettalk Layer.json")));
			Log.i("DREG", "jsonString=" + ja.toString());

		} catch (JSONException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < ja.length(); i++) {
			try {
				JSONArray addItem = new JSONArray();
				JSONArray layer = ja.getJSONArray(i);
				Log.i("DREG", "Length =" + layer.toString());
				int count = -1;
				Log.i("DREG", "Length=" + layer.length());

				for (int j = 0; j < layer.length(); j++) {
					JSONObject jo = new JSONObject();
					String file = "";
					String fileName = layer.getJSONObject(j).getString("file");
					String title = layer.getJSONObject(j).getString("title");
					Log.i("DREG", "File Name=" + fileName);
					Log.i("DREG", "Title=" + title);
					boolean isInRaw = isInRaw(fileName);

					Log.i("DREG", "isInRaw=" + isInRaw);
					if (isInRaw) {
						int imageId = getResources().getIdentifier(
								Utils.getFileName(fileName), "raw",
								getClass().getPackage().getName());
						file = "" + imageId;
					} else {
						file = getFilePath(fileName);
					}

					Log.i("DREG", "File =" + file);

					if (!file.equalsIgnoreCase("")) {
						jo.put("in_raw", isInRaw);
						jo.put("file", file);
						jo.put("title", title);
						addItem.put(jo);
					}

				}
				if (addItem != null && addItem.length() > 0) {
					items.add(addItem);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Log.i("DREG", "List length = " + items.size());
		return items;

	}

	private String getFilePath(String fileName) {
		boolean isFound = false;
		fileName = fileName + ".png";
		String path = "";

		if (!isFound) {
			File muscleFile[] = Downloader
					.getFileList(Constants.MUSLES_DIRECTORY);
			if (muscleFile != null) {
				for (int i = 0; i < muscleFile.length; i++) {
					if (muscleFile[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = muscleFile[i].getAbsolutePath();
						 return path;
					}
				}
			}
		}

		if (!isFound) {
			File hoofFile[] = Downloader.getFileList(Constants.HOOFS_DIRECTORY);
			if (hoofFile != null) {
				for (int i = 0; i < hoofFile.length; i++) {
					if (hoofFile[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = hoofFile[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File deepFile[] = Downloader
					.getFileList(Constants.DEEP_MUSCLES_DIRECTORY);

			if (deepFile != null) {
				for (int i = 0; i < deepFile.length; i++) {
					if (deepFile[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = deepFile[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File headFile[] = Downloader.getFileList(Constants.HEAD_DIRECTORY);

			if (headFile != null) {
				for (int i = 0; i < headFile.length; i++) {
					if (headFile[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = headFile[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		if (!isFound) {
			File organsFile[] = Downloader
					.getFileList(Constants.ORGANS_DIRECTORY);
			if (organsFile != null) {
				for (int i = 0; i < organsFile.length; i++) {
					if (organsFile[i].getName().equalsIgnoreCase(fileName)) {
						isFound = true;
						path = organsFile[i].getAbsolutePath();
						return path;
					}
				}
			}
		}

		return path;
	}

	private boolean isInRaw(String fileName) {
		try {
			int imageId = 0;
			imageId = getResources().getIdentifier(Utils.getFileName(fileName),
					"raw", getClass().getPackage().getName());
			if (imageId != 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}

	private void goToHomeScreen() {
		Intent backIntent = new Intent(VettalkActivity.this,
				HomeScreenActivity.class);
		backIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(backIntent);
	}

	private float x2;
	private float x1;
	private float y1;
	private float y2;
	private String DIRECTION = "no";

	String section[] = { SKIN_SIDE_LAYER, SKIN_FRONT, SKIN_BACK, MUSCLE_SIDE,
			MUSCLE_FRONT, MUSCLE_BACK, SKELETON_SIDE, SKELETON_FRONT,
			SKELETON_BACK, FORLIB_LAYER, HOOF_UNDER_LAYER, HOOF_SIDE_LAYER,
			TENDON_LAYER, JOINTS_LAYER, LIGAMENTS_LAYER, HEAD_MUSCLE_LAYER,
			HEAD_BONE_LAYER, TEETH_LAYER, INTESTINE_SIDE,
			INTESTINE_FRONT_LAYER, INTESTINE_BACK_LAYER, NECK_LAYER,
			HEART_LAYER, HEART_ARTERIES_LAYER, LARGER_ORGAN,
			CONFORMATION_FRONT, CONFORMATION_SIDE_FRONT,
			CONFORMATION_SIDE_HIND, CONFORMATION_HIND };

	private void implementVetTalk(int posX, int posY) {
		if (!isZoomed) {

			int selectionIndex = viewFlow.getSelectedItemPosition();
			if (selectionIndex >= adapter.getCount()) {
				selectionIndex = 0;
				viewFlow.setSelection(selectionIndex);
			}

			lastLayer = adapter.getTitle(selectionIndex);
			Log.i("DREG", "Last Layer=" + lastLayer);

			/* ***** Skeleton Side ******* */
			if (lastLayer.equalsIgnoreCase(SKELETON_SIDE)) {
				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {

						beginParsingForSection(1, "Skeleton");
						tappedAnimationForLayer("skeletal_head");
					}
				}

				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {

						beginParsingForSection(2, "Skeleton");
						tappedAnimationForLayer("skeletal_body");
					}
				}

				if (posX >= (175) && posX <= (405)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Skeleton");
						tappedAnimationForLayer("skeletal_body2");
					}
				}
			}

			/********** Skeleton Front *******************/
			else if (lastLayer.equals(SKELETON_FRONT)) // tapped on front view
			{
				if (posY >= (40) && posY <= (237)) {
					beginParsingForSection(4, "Skeleton");
					tappedAnimationForLayer("skeletal_front");
				} else if (posY >= (238) && posY <= (320)) {
					beginParsingForSection(5, "Skeleton");
					tappedAnimationForLayer("skeletal_front2");
				}
			}

			/******** Skeleton Back **************************/
			else if (lastLayer.equals(SKELETON_BACK)) // tapped on back view
			{
				if (posY >= (44) && posY <= (235)) {
					beginParsingForSection(6, "Skeleton");
					tappedAnimationForLayer("skeletal_back");
				} else if (posY >= (236) && posY <= (320)) {

					beginParsingForSection(7, "Skeleton");
					tappedAnimationForLayer("skeletal_back2");
				}
			}

			/******** Forelimb ********************************/
			else if (lastLayer.equals(FORLIB_LAYER)) {
				if (posX >= (100) && posX <= (380)) {
					if (posY >= (35) && posY <= (160)) {

						beginParsingForSection(1, "Forelimb");
						tappedAnimationForLayer("forelimb_top_view");

					} else if (posY >= (161) && posY <= (320)) {

						beginParsingForSection(2, "Forelimb");
						tappedAnimationForLayer("forelimb_bottom_view");
					}
				}

			}

			/********* Hoof Under *****************************/
			else if (lastLayer.equalsIgnoreCase(HOOF_UNDER_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {

						beginParsingForSection(1, "Hoof Under");
						tappedAnimationForLayer("hoofUnder_overallParts");
					}
				}

			}

			/********** Hoof Side ****************************/
			else if (lastLayer.equalsIgnoreCase(HOOF_SIDE_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (20) && posY <= (160)) {

						beginParsingForSection(1, "Hoof Side");
						tappedAnimationForLayer("hoofSide_top_view");

					} else if (posY >= (161) && posY <= (320)) {

						beginParsingForSection(2, "Hoof Side");
						tappedAnimationForLayer("hoofSide_bottom_view");
					}
				}

			}

			/************* Tendon **********************/
			else if (lastLayer.equalsIgnoreCase(TENDON_LAYER)) {

				// head
				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {
						beginParsingForSection(1, "Tendon");
						tappedAnimationForLayer("tendon_head_view");
					}

				}

				// body
				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {
						beginParsingForSection(2, "Tendon");
						tappedAnimationForLayer("tendon_body_view");
					}
				}

				// legs
				if (posX >= (175) && posX <= (405)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Downtown");
						tappedAnimationForLayer("tendon_legs_view");
					}
				}
			}

			/************* Joints **********************/
			else if (lastLayer.equalsIgnoreCase(JOINTS_LAYER)) {
				// head
				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {
						beginParsingForSection(1, "Joints");
						tappedAnimationForLayer("joints_head_view");
					}

				}

				// body
				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {
						beginParsingForSection(2, "Joints");
						tappedAnimationForLayer("joints_body_view");
					}
				}

				// legs front
				if (posX >= (50) && posX <= (240)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Joints");
						tappedAnimationForLayer("joints_legs_front_view");
					}
				}

				// legs back
				if (posX >= (241) && posX <= (480)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(4, "Joints");
						tappedAnimationForLayer("joints_legs_back_view");
					}
				}

			}

			/************* Ligaments **********************/
			else if (lastLayer.equalsIgnoreCase(LIGAMENTS_LAYER)) {

				// head
				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {
						beginParsingForSection(1, "Ligaments");
						tappedAnimationForLayer("joints_head_view");
					}

				}

				// body
				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {
						beginParsingForSection(2, "Ligaments");
						tappedAnimationForLayer("joints_body_view");
					}
				}

				// legs front

				if (posX >= (50) && posX <= (240)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Ligaments");
						tappedAnimationForLayer("joints_legs_front_view");
					}
				}

				// legs back

				if (posX >= (241) && posX <= (480)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(4, "Ligaments");
						tappedAnimationForLayer("joints_legs_back_view");
					}
				}
			}

			/************* Head Muscle **********************/
			else if (lastLayer.equalsIgnoreCase(HEAD_MUSCLE_LAYER)) {

				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Head Muscle");
						tappedAnimationForLayer("head_muscle_view");
					}
				}

			}
			/************* Head Bone **********************/
			else if (lastLayer.equalsIgnoreCase(HEAD_BONE_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Head Bone");
						tappedAnimationForLayer("head_bone_view");
					}
				}

			}
			/************* Teeth **********************/
			else if (lastLayer.equalsIgnoreCase(TEETH_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Teeth");
						tappedAnimationForLayer("teeth_view");
					}
				}

			}

			/************* Intestine Side **********************/
			else if (lastLayer.equalsIgnoreCase(INTESTINE_SIDE)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Intestine Side");
						tappedAnimationForLayer("intestine_side_view");

					}
				}

			}

			/************* Intestine Front **********************/
			else if (lastLayer.equalsIgnoreCase(INTESTINE_FRONT_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Intestine Front");
						tappedAnimationForLayer("intestine_front_view");

					}
				}

			}

			/************* Intestine Back **********************/
			else if (lastLayer.equalsIgnoreCase(INTESTINE_BACK_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Intestine Back");
						tappedAnimationForLayer("intestine_back_view");

					}
				}

			}

			/************* Neck **********************/
			else if (lastLayer.equalsIgnoreCase(NECK_LAYER)) {

				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Neck");
						tappedAnimationForLayer("neck_view");

					}
				}

			}

			/************* Heart **********************/
			else if (lastLayer.equalsIgnoreCase(HEART_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Heart");
						tappedAnimationForLayer("heart_blue_view");

					}
				}

			}

			/************* Heart Arteries **********************/
			else if (lastLayer.equalsIgnoreCase(HEART_ARTERIES_LAYER)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(2, "Heart");
						tappedAnimationForLayer("heart_red_view");

					}
				}

			}

			/************* Trachea **********************/
			else if (lastLayer.equalsIgnoreCase(LARGER_ORGAN)) {
				if (posX >= (50) && posX <= (430)) {
					if (posY >= (30) && posY <= (290)) {
						beginParsingForSection(1, "Larger Organs");
						tappedAnimationForLayer("trachea_view");

					}
				}

			}
			/************* Conformation Front **********************/
			else if (lastLayer.equalsIgnoreCase(CONFORMATION_FRONT)) {

				if (posX >= 0 && posX <= (480)) {
					if (posY >= 0 && posY <= (160)) {

						beginParsingForSection(1, "Conformation");
						tappedAnimationForLayer("conform_front_view_first");

					} else {

						beginParsingForSection(2, "Conformation");
						tappedAnimationForLayer("conform_front_view_second");

					}
				}

			}
			/************* Conformation Side Front **********************/
			else if (lastLayer.equalsIgnoreCase(CONFORMATION_SIDE_FRONT)) {
				if (posX >= 0 && posX <= (480)) {
					if (posY >= 0 && posY <= (160)) {
						beginParsingForSection(3, "Conformation");
						tappedAnimationForLayer("conform_side_front_view_second");

					} else {
						beginParsingForSection(4, "Conformation");
						tappedAnimationForLayer("conform_side_front_view_first");
					}
				}

			}

			/************* Conformation Hind **********************/
			else if (lastLayer.equalsIgnoreCase(CONFORMATION_HIND)) {

				if (posX >= 0 && posX <= (480)) {
					if (posY >= 0 && posY <= (160)) {
						beginParsingForSection(5, "Conformation");
						tappedAnimationForLayer("conform_behind_view_first");

					} else {

						beginParsingForSection(6, "Conformation");
						tappedAnimationForLayer("conform_behind_view_second");
					}
				}

			}

			/************* Conformation Side Hind **********************/
			else if (lastLayer.equalsIgnoreCase(CONFORMATION_SIDE_HIND)) {
				if (posX >= 0 && posX <= (480)) {
					if (posY >= 0 && posY <= (160)) {
						beginParsingForSection(8, "Conformation");
						tappedAnimationForLayer("conform_side_hind_view_second");
					} else {
						beginParsingForSection(7, "Conformation");
						tappedAnimationForLayer("conform_side_hind_view_first");
					}
				}

			}

			/********** Muscle Side ****************************/
			else if (lastLayer.equalsIgnoreCase(MUSCLE_SIDE)) {
				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {
						beginParsingForSection(1, "Muscle");
						tappedAnimationForLayer("muscle_head");
					}
				}

				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {
						beginParsingForSection(2, "Muscle");
						tappedAnimationForLayer("muscle_body");
					}
				}

				if (posX >= (175) && posX <= (405)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Muscle");
						tappedAnimationForLayer("muscle_body2");
					}
				}
			}

			/********** Muscle Front *******************************/
			else if (lastLayer.equalsIgnoreCase(MUSCLE_FRONT)) {
				if (posY >= (43) && posY <= (200)) {
					beginParsingForSection(4, "Muscle");
					tappedAnimationForLayer("muscle_front");
				} else if (posY >= (201) && posY <= (320)) {
					beginParsingForSection(5, "Muscle");
					tappedAnimationForLayer("muscle_front2");
				}
			}
			/********** Muscle Back **********************************/
			else if (lastLayer.equalsIgnoreCase(MUSCLE_BACK)) {

				if (posY >= (44) && posY <= (216)) {
					beginParsingForSection(6, "Muscle");
					tappedAnimationForLayer("muscle_back");
				} else if (posY >= (217) && posY <= (310)) {
					beginParsingForSection(7, "Muscle");
					tappedAnimationForLayer("muscle_back2");
				}
			}

			// /********* Skin Side **********************************/
			else if (lastLayer.equalsIgnoreCase(SKIN_SIDE_LAYER)) {

				if (posX >= (55) && posX <= (280)) {
					if (posY >= (35) && posY <= (185)) {
						beginParsingForSection(1, "Skin");
						tappedAnimationForLayer("skin_head");
					}
				}

				if (posX >= (152) && posX <= (405)) {
					if (posY >= (93) && posY <= (190)) {
						beginParsingForSection(2, "Skin");
						// [self tappedAnimationForLayer("skin_body"];
					}
				}

				if (posX >= (175) && posX <= (405)) {
					if (posY >= (185) && posY <= (315)) {
						beginParsingForSection(3, "Skin");
						tappedAnimationForLayer("skin_body2");
					}
				}
			}

			/********** Skin Front ***********************/
			else if (lastLayer.equals(SKIN_FRONT)) {
				if (posY >= (120) && posY <= (285)) {
					beginParsingForSection(4, "Skin");
					tappedAnimationForLayer("skin_front");
				}
			}

			/*********** Skin Back ***********************/
			else if (lastLayer.equalsIgnoreCase(SKIN_BACK)) {
				if (posY >= (35) && posY <= (185)) {
					beginParsingForSection(5, "Skin");
					tappedAnimationForLayer("skin_back");
				}
			}

		}

	}

	// //////////////////************////////////////////////

	public class EfficientAdapter extends BaseAdapter implements TitleProvider {
		private LayoutInflater mInflater;
		private Context context;
		private int layerIndex;

		public void switchLayer() {
			layerIndex = (layerIndex + 1) % items.size();
			notifyDataSetChanged();
		}

		public EfficientAdapter(Context context) {
			mInflater = LayoutInflater.from(context);
			this.context = context;
			this.layerIndex = 0;

		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {

					convertView = mInflater.inflate(R.layout.vettalk_content,
							null);
					holder = new ViewHolder();
					holder.imageView = (ImageView) convertView
							.findViewById(R.id.image_view);
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				final JSONObject jo = (JSONObject) getItem(position);
				Bitmap bm = null;
				if (jo.getBoolean("in_raw")) {
					bm = BitmapFactory.decodeResource(getResources(),
							jo.getInt("file"));
				} else {
					bm = BitmapFactory.decodeFile(jo.getString("file"));
				}
				Bitmap ret = Bitmap.createBitmap(imageViewWidth,
						imageViewHeight, Bitmap.Config.ARGB_8888);
				Canvas comboImage = new Canvas(ret);
				Rect r1 = new Rect(0, 0, bm.getWidth(), bm.getHeight());
				Rect r2 = new Rect(0, 0, imageViewWidth, imageViewHeight);
				comboImage.drawBitmap(bm, r1, r2, null);
				comboImage.save();
				holder.imageView.setImageBitmap(ret);

				holder.imageView.setOnTouchListener(new View.OnTouchListener() {

					@Override
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) {
							posX = (int) (event.getX() / factor);
							posY = (int) (event.getY() / factor);
						}
						return false;
					}
				});
				holder.imageView.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						clickedImage(jo, posX, posY);
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			ImageView imageView;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public int getCount() {
			if (items != null && items.size() > layerIndex) {
				return items.get(layerIndex).length();
			} else {
				return 0;
			}
		}

		public Object getItem(int position) {
			Object ret = null;
			try {
				ret = items.get(layerIndex).get(position);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return ret;
		}

		@Override
		public String getTitle(int position) {
			String ret = null;
			try {
				ret = items.get(layerIndex).getJSONObject(position)
						.getString("title");
			} catch (Exception e) {
				e.printStackTrace();
			}
			return ret;
		}
	}

	private void clickedImage(final JSONObject fileInfo, int posX, int posY) {
		try {
			viewAnimator.showNext();
			implementVetTalk(posX, posY);
			showAnimation();
			state = "normal";
			Log.i("DREG", "FILE_CLICKED=JSON" + fileInfo.toString() + ",x="
					+ posX + ",y=" + posY);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
